{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cc22ed7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1/4 1/4 1/4]\n",
      " [1/2 1/2 1/2]\n",
      " [1/4 -3/4 5/4]]\n",
      "[]\n",
      "[[1/2 -1/2 3/2]\n",
      " [1/2 1/2 1/2]]\n",
      "[[1 1]\n",
      " [-1 1]\n",
      " [0 0]]\n"
     ]
    }
   ],
   "source": [
    "#例4-11、练习4-5\n",
    "import numpy as np\n",
    "from utility import matrixSolve,Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "a1=np.array([0,1,2,3],dtype='float')\n",
    "a2=np.array([3,0,1,2],dtype='float')\n",
    "a3=np.array([2,3,0,1],dtype='float')\n",
    "b1=np.array([2,1,1,2],dtype='float')\n",
    "b2=np.array([0,-2,1,1],dtype='float')\n",
    "b3=np.array([4,4,1,3],dtype='float')\n",
    "A=np.hstack((a1.reshape(4,1),\n",
    "             a2.reshape(4,1),a3.reshape(4,1)))\n",
    "B=np.hstack((b1.reshape(4,1),\n",
    "             b2.reshape(4,1),b3.reshape(4,1)))\n",
    "X=matrixSolve(A,B)\n",
    "print(X)\n",
    "X=matrixSolve(B,A)\n",
    "print(X)\n",
    "a1=np.array([1,-1,1,-1],dtype='float')\n",
    "a2=np.array([3,1,1,3],dtype='float')\n",
    "b1=np.array([2,0,1,1],dtype='float')\n",
    "b2=np.array([1,1,0,2],dtype='float')\n",
    "b3=np.array([3,-1,2,0],dtype='float')\n",
    "A=np.hstack((a1.reshape(4,1),a2.reshape(4,1)))\n",
    "B=np.hstack((b1.reshape(4,1),\n",
    "             b2.reshape(4,1),b3.reshape(4,1)))\n",
    "X=matrixSolve(A,B)\n",
    "print(X)\n",
    "X=matrixSolve(B,A)\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "05c39ad2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2]\n",
      " [-1]\n",
      " [0]]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "#例4-12、练习4-6\n",
    "import numpy as np\n",
    "from utility import matrixSolve,Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "a1=np.array([1,1,2,2],dtype='float')\n",
    "a2=np.array([1,2,1,3],dtype='float')\n",
    "a3=np.array([1,-1,4,0],dtype='float')\n",
    "b=np.array([1,0,3,1],dtype='float')\n",
    "A=np.hstack((a1.reshape(4,1),a2.reshape(4,1)\n",
    "             ,a3.reshape(4,1)))\n",
    "X=matrixSolve(A,b.reshape(4,1))\n",
    "print(X)\n",
    "a1=np.array([1,3,2],dtype='float')\n",
    "a2=np.array([-2,-1,1],dtype='float')\n",
    "a3=np.array([3,5,2],dtype='float')\n",
    "a4=np.array([-1,-3,-2],dtype='float')\n",
    "b=np.array([1,2,3],dtype='float')\n",
    "A=np.hstack((a1.reshape(3,1),a2.reshape(3,1),a3.reshape(3,1),a4.reshape(3,1)))\n",
    "X=matrixSolve(A,b.reshape(3,1))\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6af88fb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1,a2,a3线性相关。\n",
      "(-1.0)a1+(-1.0)a2+(1.0)a3=o\n"
     ]
    }
   ],
   "source": [
    "#例4-20\n",
    "import numpy as np\n",
    "from utility import mySolve\n",
    "a1=np.array([-1,3,1]).reshape(3,1)\n",
    "a2=np.array([2,1,0]).reshape(3,1)\n",
    "a3=np.array([1,4,1]).reshape(3,1)\n",
    "o=np.zeros((3,1))\n",
    "A=np.hstack((a1,a2,a3))\n",
    "X=mySolve(A,o)\n",
    "_,t=X.shape\n",
    "if t>1:\n",
    "    print('a1,a2,a3线性相关。')\n",
    "    print('(%s)a1+(%s)a2+(%s)a3=o'\n",
    "          %(X[0,1],X[1,1],X[2,1]))\n",
    "else:\n",
    "    print('a1,a2,a3线性无关。')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "04362b31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1,a2,a3线性无关。\n"
     ]
    }
   ],
   "source": [
    "#练习4-10\n",
    "import numpy as np\n",
    "from utility import mySolve\n",
    "a1=np.array([2,3,0]).reshape(3,1)\n",
    "a2=np.array([-1,4,0]).reshape(3,1)\n",
    "a3=np.array([0,0,2]).reshape(3,1)\n",
    "o=np.zeros((3,1))\n",
    "A=np.hstack((a1,a2,a3))\n",
    "X=mySolve(A,o)\n",
    "_,t=X.shape\n",
    "if t>1:\n",
    "    print('a1,a2,a3线性相关。')\n",
    "    print('(%s)a1+(%s)a2+(%s)a3=o'\n",
    "          %(X[0,1],X[1,1],X[2,1]))\n",
    "else:\n",
    "    print('a1,a2,a3线性无关。')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e030ac98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向量组B线性无关\n"
     ]
    }
   ],
   "source": [
    "#例4-21\n",
    "import numpy as np\n",
    "from utility import rowLadder\n",
    "A=np.array([[1,2,3],\n",
    "            [2,2,4],\n",
    "            [3,1,3]],dtype='float')\n",
    "n,m=A.shape\n",
    "r,_=rowLadder(A,n,m)\n",
    "if r<n:\n",
    "    print('向量组B线性相关')\n",
    "else:\n",
    "    print('向量组B线性无关')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4f67bbf7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向量组B线性相关\n"
     ]
    }
   ],
   "source": [
    "#练习4-11\n",
    "import numpy as np\n",
    "from utility import rowLadder\n",
    "A=np.array([[1,-1,0],\n",
    "            [0,2,1],\n",
    "            [1,1,1]],dtype='float')\n",
    "n,m=A.shape\n",
    "r,_=rowLadder(A,n,m)\n",
    "if r<n:\n",
    "    print('向量组B线性相关')\n",
    "else:\n",
    "    print('向量组B线性无关')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dff79142",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大无关组：a1 a2 a4 \n",
      "a3=(-1)a1+(-1)a2+(-0)a4\n",
      "a5=(4)a1+(3)a2+(-3)a4\n"
     ]
    }
   ],
   "source": [
    "#例4-22\n",
    "import numpy as np\n",
    "from utility import maxIndepGrp\n",
    "a1=np.array([2,1,4,3],dtype='float').reshape(4,1)\n",
    "a2=np.array([-1,1,-6,6],dtype='float').reshape(4,1)\n",
    "a3=np.array([-1,-2,2,-9],dtype='float').reshape(4,1)\n",
    "a4=np.array([1,1,-2,7],dtype='float').reshape(4,1)\n",
    "a5=np.array([2,4,4,9],dtype='float').reshape(4,1)\n",
    "A=np.hstack((a1,a2,a3,a4,a5))\n",
    "_,n=A.shape\n",
    "r,ind,expr=maxIndepGrp(A)\n",
    "print('最大无关组：',end='')\n",
    "for i in range(r):\n",
    "    print('a%d'%(ind[i]+1),end=' ')\n",
    "print()\n",
    "for i in range(n-r):\n",
    "    print('a%d=(%.0f)a%d'%(ind[r+i]+1,expr[0,i],ind[0]+1),end='')\n",
    "    for j in range(1,r):\n",
    "        print('+(%.0f)a%d'%(expr[j,i],ind[j]+1),end='')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9ec7b320",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大无关组：a1 a2 a3 \n",
      "a4=(1)a1+(3)a2+(-1)a3\n",
      "a5=(0)a1+(-1)a2+(1)a3\n"
     ]
    }
   ],
   "source": [
    "#练习4-12\n",
    "import numpy as np\n",
    "from utility import maxIndepGrp\n",
    "a1=np.array([1,0,2,1],dtype='float').reshape(4,1)\n",
    "a2=np.array([1,2,0,1],dtype='float').reshape(4,1)\n",
    "a3=np.array([2,1,3,0],dtype='float').reshape(4,1)\n",
    "a4=np.array([2,5,-1,4],dtype='float').reshape(4,1)\n",
    "a5=np.array([1,-1,3,-1],dtype='float').reshape(4,1)\n",
    "A=np.hstack((a1,a2,a3,a4,a5))\n",
    "_,n=A.shape\n",
    "r,ind,expr=maxIndepGrp(A)\n",
    "print('最大无关组：',end='')\n",
    "for i in range(r):\n",
    "    print('a%d'%(ind[i]+1),end=' ')\n",
    "print()\n",
    "for i in range(n-r):\n",
    "    print('a%d=(%.0f)a%d'%(ind[r+i]+1,expr[0,i],ind[0]+1),end='')\n",
    "    for j in range(1,r):\n",
    "        print('+(%.0f)a%d'%(expr[j,i],ind[j]+1),end='')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "042dc75a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2/3]\n",
      " [-2/3]\n",
      " [-1]]\n",
      "[[4/3]\n",
      " [1]\n",
      " [2/3]]\n"
     ]
    }
   ],
   "source": [
    "#例4-30、练习4-17\n",
    "import numpy as np\n",
    "from utility import Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "P=np.array([[2,2,-1],\n",
    "            [2,-1,2],\n",
    "            [-1,2,2]],dtype='float')\n",
    "b=np.array([1,0,-4],dtype='float').reshape(3,1)\n",
    "P1=np.linalg.inv(P)\n",
    "s=np.matmul(P1,b)\n",
    "print(s)\n",
    "b=np.array([4,3,2],dtype='float').reshape(3,1)\n",
    "s=np.matmul(P1,b)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "88f4527f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-8]\n",
      " [-1]\n",
      " [5]]\n"
     ]
    }
   ],
   "source": [
    "#例4-31\n",
    "import numpy as np\n",
    "from utility import Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "A=np.array([[1,1,1],\n",
    "            [1,0,0],\n",
    "            [1,-1,1]],dtype='float')\n",
    "B=np.array([[1,2,3],\n",
    "           [2,3,4],\n",
    "           [1,4,3]],dtype='float')\n",
    "A1=np.linalg.inv(A)\n",
    "P=np.matmul(A1,B)\n",
    "P1=np.linalg.inv(P)\n",
    "x=np.array([1,1,3]).reshape(3,1)\n",
    "s=np.matmul(P1,x)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "6e73212a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-12 -26 -11]\n",
      " [6 11 3]\n",
      " [1 3 2]]\n",
      "[[13 19 43]\n",
      " [-9 -13 -30]\n",
      " [7 10 24]]\n"
     ]
    }
   ],
   "source": [
    "#练习4-18\n",
    "import numpy as np\n",
    "from utility import Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "A=np.array([[1,2,3],\n",
    "            [2,3,7],\n",
    "            [1,3,-2]],dtype='float')\n",
    "B=np.array([[3,5,1],\n",
    "           [1,2,1],\n",
    "           [4,1,-6]],dtype='float')\n",
    "A1=np.linalg.inv(A)\n",
    "P=np.matmul(A1,B)\n",
    "P1=np.linalg.inv(P)\n",
    "print(P)\n",
    "print(P1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5e2f7bb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1. -1.  1. -1.]\n",
      " [ 0.  1. -2.  3.]\n",
      " [ 0.  0.  1. -3.]\n",
      " [ 0.  0.  0.  1.]]\n",
      "[[1. 1. 1. 1.]\n",
      " [0. 1. 2. 3.]\n",
      " [0. 0. 1. 3.]\n",
      " [0. 0. 0. 1.]]\n",
      "[[10.]\n",
      " [18.]\n",
      " [17.]\n",
      " [ 6.]]\n"
     ]
    }
   ],
   "source": [
    "#例4-32、练习4-19\n",
    "import numpy as np\n",
    "from scipy.special  import comb\n",
    "def polyTransMat(n,a):\n",
    "    P=np.zeros((n,n))\n",
    "    for k in range(n):\n",
    "        for i in range(k+1):\n",
    "            P[i,k]=((-a)**(k-i))*comb(k,i)\n",
    "    return P\n",
    "n=4\n",
    "a=1\n",
    "P=polyTransMat(n,a)\n",
    "P1=np.linalg.inv(P)\n",
    "print(P)\n",
    "print(P1)\n",
    "f=np.array([3,2,-1,6]).reshape(4,1)\n",
    "print(np.matmul(P1,f))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "14878344",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.0+14.0∙x-63.0∙x**2\n",
      "5.0-24.0∙x+3.0∙x**2-28.0∙x**3\n"
     ]
    }
   ],
   "source": [
    "#\n",
    "import numpy as np\n",
    "from utility import myPoly\n",
    "def polyDifMat(n):\n",
    "    A=np.diag(np.arange(1,n))\n",
    "    A=np.vstack((np.hstack((np.zeros((n-1,1)),A)),\n",
    "                 np.zeros(n)))\n",
    "    return A\n",
    "n=4\n",
    "A=polyDifMat(n)\n",
    "f=np.array([5,-1,7,-21]).reshape(n,1)\n",
    "f1=(np.matmul(A, f).reshape(n,))\n",
    "print(myPoly(f1))\n",
    "n=5\n",
    "A=polyDifMat(n)\n",
    "f=np.array([3,5,-12,1,-7]).flatten()\n",
    "f1=(np.matmul(A, f)).reshape(n,)\n",
    "print(myPoly(f1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "88e7c817",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "值域基底：\n",
      "[[1 0]\n",
      " [-1 2]\n",
      " [1 2]\n",
      " [2 -2]]\n",
      "核基底：\n",
      "[[-2 -1]\n",
      " [-3/2 -2]\n",
      " [1 0]\n",
      " [0 1]]\n",
      "值域基底：\n",
      "[[25 31 17]\n",
      " [75 94 53]\n",
      " [75 94 54]\n",
      " [25 32 20]]\n",
      "核基底：\n",
      "[[-8/5]\n",
      " [1]\n",
      " [-2]\n",
      " [1]]\n"
     ]
    }
   ],
   "source": [
    "#例4-45、练习4-30\n",
    "import numpy as np\n",
    "from utility import maxIndepGrp, mySolve, Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "A=np.array([[1,0,2,1],\n",
    "            [-1,2,1,3],\n",
    "            [1,2,5,5],\n",
    "            [2,-2,1,-2]],dtype='float')\n",
    "r,ind,_=maxIndepGrp(A)\n",
    "print('值域基底：')\n",
    "print(A[:,ind[:r]])\n",
    "o=np.array([0,0,0,0]).reshape(4,1)\n",
    "X=mySolve(A,o)\n",
    "print('核基底：')\n",
    "print(X[:,1:])\n",
    "A=np.array([[25,31,17,43],\n",
    "            [75,94,53,132],\n",
    "            [75,94,54,134],\n",
    "            [25,32,20,48]],dtype='float')\n",
    "r,ind,_=maxIndepGrp(A)\n",
    "print('值域基底：')\n",
    "print(A[:,ind[:r]])\n",
    "o=np.array([0,0,0,0]).reshape(4,1)\n",
    "X=mySolve(A,o)\n",
    "print('核基底：')\n",
    "print(X[:,1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7ab22546",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对角阵：\n",
      "[[-1 0 0]\n",
      " [0 -1 0]\n",
      " [0 0 5]]\n",
      "基：\n",
      "[[-1 -1 1]\n",
      " [1 0 1]\n",
      " [0 1 1]]\n"
     ]
    }
   ],
   "source": [
    "#例4-46\n",
    "import numpy as np\n",
    "from utility import mySolve,Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "A=np.array([[1,2,3],\n",
    "            [2,1,3],\n",
    "            [3,3,6]],dtype='float')\n",
    "A=np.array([[1,2,2],\n",
    "            [2,1,2],\n",
    "            [2,2,1]],dtype='float')\n",
    "n,_=A.shape\n",
    "w=np.linalg.eigvals(A)\n",
    "Lambda=np.diag(np.sort(w))\n",
    "v=np.unique(np.round(w,decimals=10))\n",
    "I=np.eye(n)\n",
    "o=np.zeros((n,1))\n",
    "lam=v[0]\n",
    "P=(mySolve(lam*I-A,o))[:,1:]\n",
    "for lam in v[1:]:\n",
    "    X=mySolve(lam*I-A,o)\n",
    "    P=np.hstack((P,X[:,1:n]))\n",
    "print('对角阵：')\n",
    "print(Lambda)\n",
    "print('基：')\n",
    "print(P)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e3daf629",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对角阵：\n",
      "[[-1 0 0]\n",
      " [0 0 0]\n",
      " [0 0 9]]\n",
      "基：\n",
      "[[-1 -1 1/2]\n",
      " [1 -1 1/2]\n",
      " [0 1 1]]\n"
     ]
    }
   ],
   "source": [
    "#练习4-31\n",
    "import numpy as np\n",
    "from utility import mySolve,Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "A=np.array([[1,2,3],\n",
    "            [2,1,3],\n",
    "            [3,3,6]],dtype='float')\n",
    "n,_=A.shape\n",
    "w=np.linalg.eigvals(A)\n",
    "Lambda=np.diag(np.sort(w))\n",
    "v=np.unique(np.round(w,decimals=10))\n",
    "I=np.eye(n)\n",
    "o=np.zeros((n,1))\n",
    "lam=v[0]\n",
    "P=(mySolve(lam*I-A,o))[:,1:]\n",
    "for lam in v[1:]:\n",
    "    X=mySolve(lam*I-A,o)\n",
    "    P=np.hstack((P,X[:,1:n]))\n",
    "print('对角阵：')\n",
    "print(Lambda)\n",
    "print('基：')\n",
    "print(P)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
